package cn.hl.hm.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.vizhuo.app.constant.AppVersionConstant;
import com.vizhuo.client.base.AbstractVo;
import com.vizhuo.client.business.appmanage.constant.AppPushConstant;
import com.vizhuo.client.business.appmanage.vo.AndroidPushCustomMessage;
import com.vizhuo.client.business.appmanage.vo.AppVersionVo;
import com.vizhuo.client.business.appmanage.vo.IOSPushCustomMessage;
import com.vizhuo.client.business.match.goods.vo.GoodsVo;
import com.vizhuo.client.business.meb.mebacc.vo.MebConsignerVo;
import com.vizhuo.client.business.meb.mebacc.vo.MebDriverVo;
import com.vizhuo.client.business.meb.mebacc.vo.MebInfoVo;
import com.vizhuo.push.umeng.base.PushClient;
import com.vizhuo.push.umeng.base.PushConstant;
import com.vizhuo.utils.DataUtil;
import com.vizhuo.utils.PropertiesUtil;
import com.vizhuo.utils.StringUtil;
import com.vizhuo.utils.match.MatchUtil;
import com.vizhuo.utils.term.TermsCode;

import cn.hl.hm.service.AppPushToLogisticsService;

/**
 * 推送至 物流公司/信息部 客户端 服务实现类
 * @author dcq 作者 E-mail: ding_chuanqin@163.com
 * @date 创建时间：2016年7月1日 下午3:10:39
 * @version 1.0
 * @parameter
 * @since
 * @return
 */
@Service("appPushToLogisticsService_umeng")
public class AppPushToLogisticsServiceImpl extends UmengAppPushServiceImpl implements AppPushToLogisticsService {
    private static Logger logger = LoggerFactory.getLogger(AppPushToLogisticsServiceImpl.class);

    @Autowired
    @Qualifier("consignerPushAndroidClient")
    private PushClient consignerPushAndroidClient;

    @Autowired
    @Qualifier("consignerPushIOSClient")
    private PushClient consignerPushIOSClient;

    @Autowired
    @Qualifier("logisticsinfoPushAndroidClient")
    private PushClient logisticsinfoPushAndroidClient;

    @Autowired
    @Qualifier("driverPushAndroidClient")
    private PushClient driverPushAndroidClient;

    @Autowired
    @Qualifier("driverPushIOSClient")
    private PushClient driverPushIOSClient;

    @Autowired
    @Qualifier("logisticsinfoPushIOSClient")
    private PushClient logisticsinfoPushIOSClient;

    @Override
    public void pushForceUpgrade4Android(AppVersionVo appVersionVo) throws Exception {
        logger.info("enter pushForceUpgrade4Android of AppPushToLogisticsServiceImpl ");
        String localPath = PropertiesUtil.readValue(PropertiesUtil.CONFIG_PATH, PropertiesUtil.CONFIG_LOCALHOST_PATH);
        appVersionVo.setFileURL(localPath + appVersionVo.getFileURL());
        AndroidPushCustomMessage custom = new AndroidPushCustomMessage();
        // 由于强制升级使用的是同一个appkey广播，所以设置不同的type类型，也要推送到，但是根据不同的type有选择性的显示
        if (appVersionVo.getType() == AppVersionConstant.TYPE_ANDROID_CONSIGNER) {
            custom.setAppType(AppVersionConstant.TYPE_ANDROID_CONSIGNER);
        } else {
            custom.setAppType(AppVersionConstant.TYPE_ANDROID_ENTERPRISE);
        }
        custom.setType(AppPushConstant.OPERATION_TYPE_FORCE_UPGRADE);
        custom.setAppVersionVo(appVersionVo);
        push2Android(AppPushConstant.OPERATION_TYPE_FORCE_UPGRADE, PushConstant.DISPLAY_TYPE_MESSAGE,
                PushConstant.TYPE_BROADCAST, AppPushConstant.PUSH_UPGRADE_TITLE, "新版本v" + appVersionVo.getVersion()
                        + "点击打开应用进行更新", null, logisticsinfoPushAndroidClient, custom);
    }

    @Override
    public void pushForceUpgrade4IOS(AppVersionVo appVersionVo) throws Exception {
        logger.info("enter pushForceUpgrade4IOS of AppPushToLogisticsServiceImpl ");
        String localPath = PropertiesUtil.readValue(PropertiesUtil.CONFIG_PATH, PropertiesUtil.CONFIG_LOCALHOST_PATH);
        appVersionVo.setFileURL(localPath + appVersionVo.getFileURL());
        IOSPushCustomMessage custom = new IOSPushCustomMessage();
        if (appVersionVo.getType() == AppVersionConstant.TYPE_IOS_CONSIGNER) {
            custom.setAppType(AppVersionConstant.TYPE_IOS_CONSIGNER);
        } else {
            custom.setAppType(AppVersionConstant.TYPE_IOS_ENTERPRISE);
        }
        custom.setType(AppPushConstant.OPERATION_TYPE_FORCE_UPGRADE);
        custom.setAppVersionVo(appVersionVo);
        push2IOS(AppPushConstant.OPERATION_TYPE_FORCE_UPGRADE, PushConstant.TYPE_BROADCAST,
                "新版本v" + appVersionVo.getVersion() + "点击打开应用进行更新", null, logisticsinfoPushIOSClient, custom);
    }


    /**
     * 发货方评价承运人(司机和信息部都可)
     * 
     * @param targetPhoneNumber
     *            被评价者手机号
     * @param valuatorName
     *            评价者姓名
     * @param evalnum
     *            星级,0-5
     */
    @Override
    public void pushEvaluation(String targetPhoneNumber, String valuatorName, String evalnum, String goodsNo)
            throws Exception {
        logger.info("enter pushEvaluation of AppPushToLogisticsServiceImpl ");
        if (StringUtil.isBlank(goodsNo)) {
            goodsNo = "00000000";
        }
        if (StringUtil.isEmpty(targetPhoneNumber)) {
            throw new Exception("被评价者手机号不可为空");
        }
        if (StringUtil.isEmpty(valuatorName)) {
            throw new Exception("评价者姓名不可为空");
        }
        List<String> phoneNumbers = new ArrayList<String>();
        phoneNumbers.add(targetPhoneNumber);
        DataUtil.choosePhoneByOnLine(phoneNumbers);// 剔除不在线的用户
        logger.info("剔除不在线用户后:" + phoneNumbers.toString());
        DataUtil.choosePushSetOn(phoneNumbers, TermsCode.APP_PUSHINSTALL_TYPE_LIST.TOEVALREMIND_STATE);// 剔除关掉了推送设置的用户
        logger.info("剔除关掉了推送设置的用户后:" + phoneNumbers.toString());
        if (phoneNumbers == null || phoneNumbers.isEmpty()) {
            return;
        }
        String token = "";
//        String token = appDeviceService.selectToken(targetPhoneNumber, AppVersionConstant.TYPE_ANDROID_ENTERPRISE);
        int appType = AppVersionConstant.TYPE_ANDROID_ENTERPRISE;
//        if (StringUtil.isEmpty(token)) {
//            token = appDeviceService.selectToken(targetPhoneNumber, AppVersionConstant.TYPE_ANDROID_DRIVER);
//            appType = AppVersionConstant.TYPE_ANDROID_DRIVER;
//        }
//        if (StringUtil.isEmpty(token)) {
//            token = appDeviceService.selectToken(targetPhoneNumber, AppVersionConstant.TYPE_IOS_ENTERPRISE);
//            appType = AppVersionConstant.TYPE_IOS_ENTERPRISE;
//        }
//        if (StringUtil.isEmpty(token)) {
//            token = appDeviceService.selectToken(targetPhoneNumber, AppVersionConstant.TYPE_IOS_DRIVER);
//            appType = AppVersionConstant.TYPE_IOS_DRIVER;
//        }
//        if (StringUtil.isEmpty(token)) {
//            token = appDeviceService.selectToken(targetPhoneNumber, AppVersionConstant.TYPE_ANDROID_CONSIGNER);
//            appType = AppVersionConstant.TYPE_ANDROID_CONSIGNER;
//        }
//        if (StringUtil.isEmpty(token)) {
//            token = appDeviceService.selectToken(targetPhoneNumber, AppVersionConstant.TYPE_IOS_CONSIGNER);
//            appType = AppVersionConstant.TYPE_IOS_CONSIGNER;
//        }
        if (StringUtil.isEmpty(token)) {
            throw new Exception("被评价者的手机号" + targetPhoneNumber + "没有找寻到对应的数据，无法开始推送服务");
        }
        if (appType == AppVersionConstant.TYPE_ANDROID_ENTERPRISE || appType == AppVersionConstant.TYPE_ANDROID_DRIVER
                || appType == AppVersionConstant.TYPE_ANDROID_CONSIGNER) {
            AndroidPushCustomMessage custom = new AndroidPushCustomMessage();
            custom.setAppType(appType);
            custom.setType(AppPushConstant.OPERATION_TYPE_EVALUATION);
            String pushContent = "";
            if ("5".equals(evalnum)) {// 好评
                pushContent = valuatorName + "发货人给出" + evalnum + "星好评，订单尾号" + goodsNo.substring(goodsNo.length() - 6)
                        + "。";
            } else if ("0".equals(evalnum)) {// 差评
                pushContent = valuatorName + "发货人给出差评,1小时内可沟通修改，订单尾号" + goodsNo.substring(goodsNo.length() - 6) + "。";
            } else {// 都不是
                pushContent = valuatorName + "发货人给出" + evalnum + "星评价，订单尾号" + goodsNo.substring(goodsNo.length() - 6)
                        + "。";
            }
            custom.setContext(pushContent);
            if (appType == AppVersionConstant.TYPE_ANDROID_ENTERPRISE) {
                push2Android(AppPushConstant.OPERATION_TYPE_EVALUATION, PushConstant.DISPLAY_TYPE_MESSAGE,
                        PushConstant.TYPE_UNICAST, AppPushConstant.PUSH_EVALUATION_CON_TITLE, pushContent, token,
                        logisticsinfoPushAndroidClient, custom);
            } else if (appType == AppVersionConstant.TYPE_ANDROID_DRIVER) {
                push2Android(AppPushConstant.OPERATION_TYPE_EVALUATION, PushConstant.DISPLAY_TYPE_MESSAGE,
                        PushConstant.TYPE_UNICAST, AppPushConstant.PUSH_EVALUATION_CON_TITLE, pushContent, token,
                        driverPushAndroidClient, custom);
            } else {
                push2Android(AppPushConstant.OPERATION_TYPE_EVALUATION, PushConstant.DISPLAY_TYPE_MESSAGE,
                        PushConstant.TYPE_UNICAST, AppPushConstant.PUSH_EVALUATION_CON_TITLE, pushContent, token,
                        consignerPushAndroidClient, custom);
            }

        } else {
            IOSPushCustomMessage customIOS = new IOSPushCustomMessage();
            customIOS.setAppType(appType);
            customIOS.setType(AppPushConstant.OPERATION_TYPE_EVALUATION);
            String pushContent = "";
            if ("5".equals(evalnum)) {// 好评
                pushContent = valuatorName + "发货人给出" + evalnum + "星好评，订单尾号" + goodsNo.substring(goodsNo.length() - 6)
                        + "。";
            } else if ("0".equals(evalnum)) {// 差评
                pushContent = valuatorName + "发货人给出差评,1小时内可沟通修改，订单尾号" + goodsNo.substring(goodsNo.length() - 6) + "。";
            } else {// 都不是
                pushContent = valuatorName + "发货人给出" + evalnum + "星评价，订单尾号" + goodsNo.substring(goodsNo.length() - 6)
                        + "。";
            }
            customIOS.setContext(pushContent);
            if (appType == AppVersionConstant.TYPE_IOS_DRIVER) {
                push2IOS(AppPushConstant.OPERATION_TYPE_EVALUATION, PushConstant.TYPE_UNICAST,
                        AppPushConstant.PUSH_EVALUATION_CON_TITLE, token, driverPushIOSClient, customIOS);
            } else if (appType == AppVersionConstant.TYPE_IOS_ENTERPRISE) {
                push2IOS(AppPushConstant.OPERATION_TYPE_EVALUATION, PushConstant.TYPE_UNICAST,
                        AppPushConstant.PUSH_EVALUATION_CON_TITLE, token, logisticsinfoPushIOSClient, customIOS);
            } else {
                push2IOS(AppPushConstant.OPERATION_TYPE_EVALUATION, PushConstant.TYPE_UNICAST,
                        AppPushConstant.PUSH_EVALUATION_CON_TITLE, token, consignerPushIOSClient, customIOS);
            }

        }
    }

    // 抢单
    @Override
    public void pushApplyOrder(GoodsVo goodsVo) throws Exception {
        logger.info("enter pushApplyOrder of AppPushToLogisticsServiceImpl ");
        if (goodsVo == null) {
            throw new Exception("货源/用车信息为空，无法开始推送服务");
        }
        String token = "";
//        String token = appDeviceService.selectToken(goodsVo.getAccount(), AppVersionConstant.TYPE_ANDROID_CONSIGNER);
        int appType = AppVersionConstant.TYPE_ANDROID_CONSIGNER;
        if (StringUtil.isEmpty(token)) {
//            token = appDeviceService.selectToken(goodsVo.getAccount(), AppVersionConstant.TYPE_IOS_CONSIGNER);
            appType = AppVersionConstant.TYPE_IOS_CONSIGNER;
        }
        if (StringUtil.isEmpty(token)) {
            throw new Exception("商户的手机号" + goodsVo.getAccount() + "没有找寻到对应的数据，无法开始推送服务");
        }
        // String pushText = MatchUtil.getGoodsRemark(goodsVo);
        String carriyerType = "";
        if ("1".equals(goodsVo.getCarrierAccountType())) {
            // 司机
            carriyerType = "司机";
        } else {
            // 物流公司
            carriyerType = "物流公司";
        }
        String pushText = carriyerType + goodsVo.getCarrierName() + "抢了您尾号为"
                + goodsVo.getGoodsNo().substring(goodsVo.getGoodsNo().length() - AppPushConstant.PUSH_GOODS_NO_INDEX)
                + "的订单，赶快联系他吧！";
        if (appType == AppVersionConstant.TYPE_ANDROID_CONSIGNER) {
            AndroidPushCustomMessage custom = new AndroidPushCustomMessage();
            custom.setAppType(AppVersionConstant.TYPE_ANDROID_CONSIGNER);
            custom.setType(AppPushConstant.OPERATION_TYPE_APPLY_ORDER);
            custom.setContext(pushText);
            custom.setGoodVo(goodsVo);
            push2Android(AppPushConstant.OPERATION_TYPE_APPLY_ORDER, PushConstant.DISPLAY_TYPE_MESSAGE,
                    PushConstant.TYPE_UNICAST, AppPushConstant.PUSH_APPLY_ORDER_TITLE, pushText, token,
                    consignerPushAndroidClient, custom);
        } else {
            IOSPushCustomMessage customIOS = new IOSPushCustomMessage();
            customIOS.setAppType(AppVersionConstant.TYPE_IOS_CONSIGNER);
            customIOS.setType(AppPushConstant.OPERATION_TYPE_APPLY_ORDER);
            customIOS.setContext(pushText);
            customIOS.setGoodVo(goodsVo);
            push2IOS(AppPushConstant.OPERATION_TYPE_APPLY_ORDER, PushConstant.TYPE_UNICAST, pushText, token,
                    consignerPushIOSClient, customIOS);
        }

    }

    // 返回成功推送数
    @Override
    public int pushGoodToDifferenceCity(GoodsVo goodsVo) throws Exception {
        logger.info("enter pushGoodToDifferenceCity of AppPushToLogisticsServiceImpl ");
        int pushSuccessCount = 0;
//        MebInfoQuery q = new MebInfoQuery();
//        q.setRunLineFromProviceCode(goodsVo.getStartProvice());
//        q.setRunLineFromCityCode(goodsVo.getStartCity());
//        q.setRunLineFromCountryCode(goodsVo.getStartCounty());
//        q.setRunLineToProviceCode(goodsVo.getEndProvice());
//        q.setRunLineToCityCode(goodsVo.getEndCity());
//        q.setRunLineToCountryCode(goodsVo.getEndCounty());
//        q.setMebAccId(goodsVo.getAccountId());// 发货人的账号id

        List<String> phoneNumbers = null;//mebInfoIMapper.selectAccountByLine(q);
        DataUtil.choosePhoneByOnLine(phoneNumbers);// 剔除不在线的用户
        logger.info("剔除不在线用户后:" + phoneNumbers.toString());
        DataUtil.choosePushSetOn(phoneNumbers, TermsCode.APP_PUSHINSTALL_TYPE_LIST.GOODSREMIND_STATE);// 剔除关掉了推送设置的用户
        logger.info("剔除关掉了推送设置的用户后:" + phoneNumbers.toString());
        if (phoneNumbers.size() > 0) {
            pushSuccessCount = pushGoodToInfo(goodsVo, phoneNumbers);
        }
        logger.info("left pushGoodToDifferenceCity of AppPushToLogisticsServiceImpl ");
        return pushSuccessCount;

    }

    private int pushGoodToInfo(GoodsVo goodsVo, List<String> phoneNumbers) throws Exception {
        logger.info("enter pushGoodToInfo of AppPushToLogisticsServiceImpl ");
        int pushSuccessCount = 0;
        List<String> androidTokenList = getAndroidTokens(phoneNumbers);
        List<String> iosTokenList = getIosTokens(phoneNumbers);
        if (androidTokenList.size() > 0) {
            pushSuccessCount += androidTokenList.size();
            pushFindDriverInfo2Android(goodsVo, androidTokenList);
        }
        if (iosTokenList.size() > 0) {
            pushSuccessCount += iosTokenList.size();
            pushFindDriverInfo2IOS(goodsVo, iosTokenList);
        }
        // 另外再将货源推送数据记录到当日货源推送表中
        if (phoneNumbers != null && !phoneNumbers.isEmpty()) {
            for (int i = 0; i < phoneNumbers.size(); i++) {
                if (StringUtil.isBlank(phoneNumbers.get(i))) {
                    continue;
                }
//                AppDayGoodspush appDayGoodspush = addDayGoodspush(goodsVo, phoneNumbers.get(i), i);
            }
        }
        return pushSuccessCount;
    }

//    private AppDayGoodspush addDayGoodspush(GoodsVo goodsVo, String phoneNumber, int i) {
//        AppDayGoodspush appDayGoodspush = new AppDayGoodspush();
//        try {
//            appDayGoodspush.setAccountId(goodsVo.getAccountId());
//            appDayGoodspush.setGoodsId(goodsVo.getId());
//            appDayGoodspush.setGoodsNo(goodsVo.getGoodsNo());
//            appDayGoodspush.setGoodsRemark(goodsVo.getGoodsRemark());
//            appDayGoodspush.setReceiveAccount(phoneNumber);
//            appPushTaskMapper.insertAppDayGoodsPush(appDayGoodspush);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return appDayGoodspush;
//    }

    private void pushFindDriverInfo2IOS(GoodsVo goodsVo, List<String> iosTokenList) throws Exception {
        String pushText = MatchUtil.getGoodsRemark(goodsVo);
        IOSPushCustomMessage customIOS = new IOSPushCustomMessage();
        customIOS.setAppType(AppVersionConstant.TYPE_IOS_ENTERPRISE);
        customIOS.setType(AppPushConstant.OPERATION_TYPE_GOOD_TO_INFO);
        customIOS.setContext(pushText);
        customIOS.setGoodVo(goodsVo);
        for (String tokenStr : iosTokenList) {
            push2IOS(AppPushConstant.OPERATION_TYPE_GOOD_TO_INFO, PushConstant.TYPE_LISTCAST, goodsVo.getGoodsRemark(),
                    tokenStr, logisticsinfoPushIOSClient, customIOS);
        }
    }

    private void pushFindDriverInfo2Android(GoodsVo goodsVo, List<String> androidTokenList) throws Exception {
        String pushText = MatchUtil.getGoodsRemark(goodsVo);
        AndroidPushCustomMessage custom = new AndroidPushCustomMessage();
        custom.setAppType(AppVersionConstant.TYPE_ANDROID_ENTERPRISE);
        custom.setType(AppPushConstant.OPERATION_TYPE_GOOD_TO_INFO);
        custom.setContext(pushText);
        custom.setGoodVo(goodsVo);
        for (String tokenStr : androidTokenList) {
            push2Android(AppPushConstant.OPERATION_TYPE_GOOD_TO_INFO, PushConstant.DISPLAY_TYPE_MESSAGE,
                    PushConstant.TYPE_LISTCAST, AppPushConstant.PUSH_FIND_DRIVER_INFO_TITLE, pushText, tokenStr,
                    logisticsinfoPushAndroidClient, custom);
        }
    }

    private List<String> getIosTokens(List<String> phoneNumbers) {
        List<String> iosTokenList = new ArrayList<String>();
        StringBuilder tokenBuild = new StringBuilder();
        int tokenCount = 0;
        tokenBuild.append("," + "725f0aa62e90ab128c30dad268a29d2b443e017e39bf61ae63e3fc6558bb70e1");
        
//        for (String phoneNumber : phoneNumbers) {
//            String token = appDeviceService.selectToken(phoneNumber, AppVersionConstant.TYPE_IOS_ENTERPRISE);
//            if (StringUtil.isNotEmpty(token)) {
//                tokenBuild.append("," + token);
//                tokenCount++;
//            }
//            if (tokenCount == 500) {
//                iosTokenList.add(tokenBuild.toString().substring(1));
//                tokenCount = 0;
//            }
//        }
        if (tokenBuild.length() > 1) {
            iosTokenList.add(tokenBuild.toString().substring(1));
        }

        return iosTokenList;
    }

    private List<String> getAndroidTokens(List<String> phoneNumbers) {

        StringBuilder tokenBuild = new StringBuilder();
        int tokenCount = 0;
        tokenBuild.append("," + "ApKGZXYNJjdl7sxXy31hz8VUSe2bv8bJnDB5evx9vMmq");
        List<String> androidTokenList = new ArrayList<String>();
//        for (String phoneNumber : phoneNumbers) {
//            String token = appDeviceService.selectToken(phoneNumber, AppVersionConstant.TYPE_ANDROID_ENTERPRISE);
//            if (StringUtil.isNotEmpty(token)) {
//                tokenBuild.append("," + token);
//                tokenCount++;
//            }
//            if (tokenCount == 500) {
//                androidTokenList.add(tokenBuild.toString().substring(1));
//                tokenCount = 0;
//            }
//        }
        //if (tokenBuild.length() > 1) {
            androidTokenList.add(tokenBuild.toString().substring(1));
        //}

        return androidTokenList;
    }

    // 审核信息推送
    @Override
    public void pushMsgByAccount(String phoneNumber, String pushTitle, String pushText, String accountType,
            AbstractVo abstractVo) throws Exception {
        logger.info("enter pushMsgByAccount of AppPushToLogisticsServiceImpl ");
        if (StringUtil.isBlank(phoneNumber)) {
            return;
        }
        List<String> phoneNumbers = new ArrayList<String>();
        phoneNumbers.add(phoneNumber);
        //DataUtil.choosePhoneByOnLine(phoneNumbers);// 剔除不在线的用户
        logger.info("剔除不在线用户后:" + phoneNumbers.toString());
        if (phoneNumbers == null || phoneNumbers.isEmpty()) {
            return;
        }
        List<String> androidTokenList = getAndroidTokens(phoneNumbers);
        List<String> iosTokenList = getIosTokens(phoneNumbers);
        if (StringUtil.isBlank(pushTitle)) {
            pushTitle = AppPushConstant.PUSH_SYS_MSG_TITLE;
        }
        if (androidTokenList != null && androidTokenList.size() > 0) {
            AndroidPushCustomMessage custom = new AndroidPushCustomMessage();
            custom.setAppType(AppVersionConstant.TYPE_ANDROID_ENTERPRISE);
            custom.setType(AppPushConstant.OPERATION_TYPE_UPDATE_MEB_INFO);
            custom.setContext(pushText);
            try {
                if (abstractVo != null) {
                    if (TermsCode.CLIENT_TYPE_LIST.CONSUGNOR.getValue().equals(accountType)) {
                        custom.setMebConsignerVo((MebConsignerVo) abstractVo);
                    } else if (TermsCode.CLIENT_TYPE_LIST.INFO.getValue().equals(accountType)) {
                        custom.setMebInfoVo((MebInfoVo) abstractVo);
                    } else if (TermsCode.CLIENT_TYPE_LIST.DRIVER.getValue().equals(accountType)) {
                        custom.setMebDriverVo((MebDriverVo) abstractVo);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            push2Android(AppPushConstant.OPERATION_TYPE_UPDATE_MEB_INFO, PushConstant.DISPLAY_TYPE_MESSAGE,
                    PushConstant.TYPE_UNICAST, pushTitle, pushText, androidTokenList.get(0),
                    logisticsinfoPushAndroidClient, custom);
        }
        if (iosTokenList != null && iosTokenList.size() > 0) {
            IOSPushCustomMessage customIOS = new IOSPushCustomMessage();
            customIOS.setAppType(AppVersionConstant.TYPE_IOS_ENTERPRISE);
            customIOS.setType(AppPushConstant.OPERATION_TYPE_UPDATE_MEB_INFO);
            customIOS.setContext(pushText);
            try {
                if (abstractVo != null) {
                    if (TermsCode.CLIENT_TYPE_LIST.CONSUGNOR.getValue().equals(accountType)) {
                        customIOS.setMebConsignerVo((MebConsignerVo) abstractVo);
                    } else if (TermsCode.CLIENT_TYPE_LIST.INFO.getValue().equals(accountType)) {
                        customIOS.setMebInfoVo((MebInfoVo) abstractVo);
                    } else if (TermsCode.CLIENT_TYPE_LIST.DRIVER.getValue().equals(accountType)) {
                        customIOS.setMebDriverVo((MebDriverVo) abstractVo);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            push2IOS(AppPushConstant.OPERATION_TYPE_UPDATE_MEB_INFO, PushConstant.TYPE_UNICAST, pushText,
                    iosTokenList.get(0), logisticsinfoPushIOSClient, customIOS);
        }

    }

    // 推送消息-单播
    @Override
    public void pushSysMsgByAccount(int operationType, String phoneNumber, String pushTitle, String pushText)
            throws Exception {
        logger.info("enter pushSysMsgByAccount of AppPushToLogisticsServiceImpl ");
        if (StringUtil.isBlank(phoneNumber)) {
            return;
        }
        List<String> phoneNumbers = new ArrayList<String>();
        phoneNumbers.add(phoneNumber);

        List<String> androidTokenList = getAndroidTokens(phoneNumbers);
        List<String> iosTokenList = getIosTokens(phoneNumbers);
        if (StringUtil.isBlank(pushTitle)) {
            pushTitle = AppPushConstant.PUSH_SYS_MSG_TITLE;
        }
        if (androidTokenList != null && androidTokenList.size() > 0) {
            AndroidPushCustomMessage custom = new AndroidPushCustomMessage();
            custom.setAppType(AppVersionConstant.TYPE_ANDROID_ENTERPRISE);
            custom.setType(operationType);
            custom.setContext(pushText);
            push2Android(operationType, PushConstant.DISPLAY_TYPE_MESSAGE, PushConstant.TYPE_UNICAST, pushTitle,
                    pushText, androidTokenList.get(0), logisticsinfoPushAndroidClient, custom);
        }
        if (iosTokenList != null && iosTokenList.size() > 0) {
            IOSPushCustomMessage customIOS = new IOSPushCustomMessage();
            customIOS.setAppType(AppVersionConstant.TYPE_IOS_ENTERPRISE);
            customIOS.setType(operationType);
            customIOS.setContext(pushText);
            push2IOS(operationType, PushConstant.TYPE_UNICAST, pushText, iosTokenList.get(0),
                    logisticsinfoPushIOSClient, customIOS);
        }

    }

    // 系统消息-所有设备
    @Override
    public void pushSysMsgAll(String title, String pushText) throws Exception {

        pushSysMsgAll(AppPushConstant.OPERATION_TYPE_SYS_MSG_ALL, title, pushText);
    }

    // 系统消息-所有设备
    @Override
    public void pushSysMsgAll(int operationType, String title, String pushText) throws Exception {
        logger.info("enter pushSysMsgAll of AppPushToLogisticsServiceImpl ");
        if (StringUtil.isBlank(title) || StringUtil.isBlank(pushText)) {
            return;
        }
        AndroidPushCustomMessage custom = new AndroidPushCustomMessage();
        custom.setAppType(AppVersionConstant.TYPE_ANDROID_ENTERPRISE);
        custom.setType(operationType);
        custom.setContext(pushText);
        push2Android(operationType, PushConstant.DISPLAY_TYPE_MESSAGE, PushConstant.TYPE_BROADCAST, title, pushText,
                null, logisticsinfoPushAndroidClient, custom);
        IOSPushCustomMessage customIOS = new IOSPushCustomMessage();
        customIOS.setAppType(AppVersionConstant.TYPE_IOS_ENTERPRISE);
        customIOS.setType(operationType);
        customIOS.setContext(pushText);
        push2IOS(operationType, PushConstant.TYPE_BROADCAST, pushText, null, logisticsinfoPushIOSClient, customIOS);
    }

    @Override
    public void pushTest(int operationType, String title, String context) {

//        List<MebAcc> mebAccList = mebAccIMapper.selectAllEntity();
        List<String> phoneNumbers = new ArrayList<String>();
//        for (MebAcc mebAcc : mebAccList) {
//            phoneNumbers.add(mebAcc.getAccount());
//        }
        phoneNumbers.add("15101633389");
        phoneNumbers.add("18911840331");
        // DataUtil.choosePhoneByOnLine(phoneNumbers);// 剔除不在线的用户
        List<String> androidTokenList = getAndroidTokens(phoneNumbers);
        List<String> iosTokenList = getIosTokens(phoneNumbers);
        try {
            AndroidPushCustomMessage custom = new AndroidPushCustomMessage();
            custom.setType(operationType);
            custom.setContext(context);
            for (String tokenStr : androidTokenList) {
                push2Android(operationType, PushConstant.DISPLAY_TYPE_MESSAGE, PushConstant.TYPE_LISTCAST, title,
                        context, tokenStr, logisticsinfoPushAndroidClient, custom);
            }
            IOSPushCustomMessage customIOS = new IOSPushCustomMessage();
            customIOS.setType(operationType);
            customIOS.setContext(context);
            for (String tokenStr : iosTokenList) {
                push2IOS(operationType, PushConstant.TYPE_LISTCAST, context, tokenStr, logisticsinfoPushIOSClient,
                        customIOS);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    // 指派物流公司推送给物流公司
    @Override
    public void pushOrderToLogistics(String phoneNumber, GoodsVo goodsVo) throws Exception {
        logger.info("enter pushOrderToLogistics of AppPushToLogisticsServiceImpl ");
        List<String> phoneNumbers = new ArrayList<String>();
        phoneNumbers.add(phoneNumber);
        DataUtil.choosePhoneByOnLine(phoneNumbers);// 剔除不在线的用户
        logger.info("剔除不在线用户后:" + phoneNumbers.toString());
        DataUtil.choosePushSetOn(phoneNumbers, TermsCode.APP_PUSHINSTALL_TYPE_LIST.GOODSREMIND_STATE);// 剔除关掉了推送设置的用户
        logger.info("剔除关掉了推送设置的用户后:" + phoneNumbers.toString());
        if (phoneNumbers == null || phoneNumbers.isEmpty()) {
            return;
        }

        String token = "";//appDeviceService.selectToken(phoneNumber, AppVersionConstant.TYPE_ANDROID_ENTERPRISE);
        int appType = AppVersionConstant.TYPE_ANDROID_ENTERPRISE;
        // String goodInfo = MatchUtil.getGoodsRemark(goodsVo);
        if (StringUtil.isEmpty(token)) {
//            token = appDeviceService.selectToken(phoneNumber, AppVersionConstant.TYPE_IOS_ENTERPRISE);
            appType = AppVersionConstant.TYPE_IOS_ENTERPRISE;
        }
        if (StringUtil.isEmpty(token)) {
            throw new Exception("该物流公司的手机号" + phoneNumber + "没有找寻到对应的数据，无法开始推送服务");
        }
        String pushText = MatchUtil.getGoodsRemark(goodsVo);
        if (appType == AppVersionConstant.TYPE_ANDROID_ENTERPRISE) {
            AndroidPushCustomMessage custom = new AndroidPushCustomMessage();
            custom.setAppType(AppVersionConstant.TYPE_ANDROID_ENTERPRISE);
            custom.setType(AppPushConstant.OPERATION_TYPE_TO_ASSIGNED_LOGISTICS);
            custom.setContext(pushText);
            custom.setGoodVo(goodsVo);
            push2Android(AppPushConstant.OPERATION_TYPE_TO_ASSIGNED_LOGISTICS, PushConstant.DISPLAY_TYPE_MESSAGE,
                    PushConstant.TYPE_UNICAST, AppPushConstant.PUSH_CHOOSE_DRIVER_SUCCESS_TITLE, pushText, token,
                    logisticsinfoPushAndroidClient, custom);
        } else {
            IOSPushCustomMessage customIOS = new IOSPushCustomMessage();
            customIOS.setAppType(AppVersionConstant.TYPE_IOS_ENTERPRISE);
            customIOS.setType(AppPushConstant.OPERATION_TYPE_TO_ASSIGNED_LOGISTICS);
            customIOS.setContext(pushText);
            customIOS.setGoodVo(goodsVo);
            push2IOS(AppPushConstant.OPERATION_TYPE_TO_ASSIGNED_LOGISTICS, PushConstant.TYPE_UNICAST,
                    AppPushConstant.PUSH_CHOOSE_DRIVER_SUCCESS_TITLE, token, logisticsinfoPushIOSClient, customIOS);
        }
        logger.info("left pushOrderToLogistics of AppPushToLogisticsServiceImpl ");
    }

    public static void main(String[] args) {
        System.out.println(System.currentTimeMillis());
    }

}
